package com.phone;

import com.phone.exception.ParameterException;
import com.phone.utils.TextUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by tisen on 2017/9/7.
 * 处理发起请求的参数设置
 */
public class ParameterCheck {
    private static final String CHARACTER_ENCODING = "UTF-8";

    private HttpServletRequest request;

    public ParameterCheck(HttpServletRequest request) throws UnsupportedEncodingException {
        this.request = request;
        this.request.setCharacterEncoding(CHARACTER_ENCODING);
    }

    public void setParameterRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public String getParameterString(String key, boolean nullable) throws Exception {
        String value = request.getParameter(key);
        if (!nullable && TextUtil.textIsEmpty(value))
            throw new ParameterException(ParameterException.PARAMETER_IS_EMPTY, key);
        return value;
    }

    public String getParameterString(String key, boolean nullable, String defaultString) throws Exception {
        String value = request.getParameter(key);
        if (!nullable && TextUtil.textIsEmpty(value))
            throw new ParameterException(ParameterException.PARAMETER_IS_EMPTY, key);
        if (TextUtil.textIsEmpty(value))
            return defaultString;
        else
            return value;
    }

    public int getParameterInt(String key, boolean nullable, int defValue) throws Exception {
        String value = getParameterString(key, nullable);
        if (TextUtil.textIsEmpty(value)) {
            return defValue;
        } else {
            try {
                int i = Integer.parseInt(value);
                return i;
            } catch (NumberFormatException e) {
                throw new ParameterException(ParameterException.PARAMETER_FORMAT_WRONG, key);
            }
        }
    }

    public long getParameterLong(String key, boolean nullable, long defValue) throws Exception {
        String value = getParameterString(key, nullable);
        if (TextUtil.textIsEmpty(value)) {
            return defValue;
        } else {
            try {
                long l = Long.parseLong(value);
                return l;
            } catch (NumberFormatException e) {
                throw new ParameterException(ParameterException.PARAMETER_FORMAT_WRONG, key);
            }
        }
    }

    public float getParameterFloat(String key, boolean nullable, float defValue) throws Exception {
        String value = getParameterString(key, nullable);
        if (TextUtil.textIsEmpty(value)) {
            return defValue;
        } else {
            try {
                float f = Float.parseFloat(value);
                return f;
            } catch (NumberFormatException e) {
                throw new ParameterException(ParameterException.PARAMETER_FORMAT_WRONG, key);
            }
        }
    }

    public boolean getParameterBoolean(String key, boolean nullable, boolean defValue) throws Exception {
        String value = getParameterString(key, nullable);
        if (TextUtil.textIsEmpty(value)) {
            return defValue;
        } else {
            try {
                boolean f = Boolean.parseBoolean(value);
                return f;
            } catch (NumberFormatException e) {
                throw new ParameterException(ParameterException.PARAMETER_FORMAT_WRONG, key);
            }
        }
    }
}
